<?php
/**
 * 创建角色
 * 2018/08/01
 * cxm
 * @return void
 */
use Illuminate\Database\Seeder;

use App\Models\AdminRole;
use App\Models\AdminUserRole;
use App\Models\AdminUser;


class AdminUserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $adminUser = AdminUser::where('user_type', 1)->first();

        if (!isset($adminUser->id)) {
            // 创建超级管理员 (平台)
            $adminUser = new AdminUser();
            $adminUser->user_type = 1;
            $adminUser->username = 'admin';
            $adminUser->password =  bcrypt(strtoupper(md5('admin2020')));
            $adminUser->name = '超级管理员';
            $adminUser->save();
        }

        // 查询内置角色
        $adminRole = AdminRole::where('name', '超级管理员')->first();
        if (!isset($adminRole->id)) {
            // 创建内置角色
            $adminRole = new AdminRole();
            $adminRole->name = '超级管理员';
            $adminRole->desc = '所有权限';
            $adminRole->save();
        }

        // 查询管理员角色关联
        $adminRoleUser = AdminUserRole::where('role_id', $adminRole->id)->where('user_id', $adminUser->id)->first();
        if (!isset($adminRoleUser->user_id)) {
            // 创建管理员角色关联
            $model = new AdminUserRole();
            $model->role_id = $adminRole->id;
            $model->user_id = $adminUser->id;
            $model->save();
        }

    }
}
